package com.danco.training.zaneuskaya.faces;

import java.io.Serializable;
import java.util.Date;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.danco.training.zaneuskaya.domain.City;
import com.danco.training.zaneuskaya.domain.Country;
import com.danco.training.zaneuskaya.domain.Education;
import com.danco.training.zaneuskaya.domain.EducationType;
import com.danco.training.zaneuskaya.service.EducationService;
import com.danco.training.zaneuskaya.service.UserService;
import com.danco.training.zaneuskaya.service.exception.UserException;

/**
 * @author Zaneuskaya Aliaksandra
 *
 */
@Component
@Scope("request")
public class EducationBean implements Serializable {

	private static final String USER_MAIN = "/user/main";

	private static final String ADD_USER_EDUCATION_FAILED = "Add user education failed";

	private static final String SET_USER_BEAN_FROM_EDUCATION_BEAN = "Set user bean from education bean";

	private static final String GET_USER_BEAN_FROM_EDUCATION_BEAN = "Get user bean from education bean";

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private static final Logger LOGGER = Logger.getLogger(EducationBean.class);

	@Autowired
	private UserBean userBean;

	private Date startDate;

	private Date endDate;

	private String institution;

	private Country country;

	private City city;

	private EducationType type;

	private Education currentEducation;

	@Autowired
	private UserService userService;

	@Autowired
	private EducationService educationService;

	public Education getCurrentEducation() {
		return currentEducation;
	}

	public void setCurrentEducation(Education currentEducation) {
		this.currentEducation = currentEducation;
	}

	public UserBean getUserBean() {
		LOGGER.trace(GET_USER_BEAN_FROM_EDUCATION_BEAN);
		return userBean;
	}

	public void setUserBean(UserBean userBean) {
		LOGGER.trace(SET_USER_BEAN_FROM_EDUCATION_BEAN);
		this.userBean = userBean;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public EducationService getEducationService() {
		return educationService;
	}

	public void setEducationService(EducationService educationService) {
		this.educationService = educationService;
	}

	public Date getStartDate() {
		return startDate;
	}

	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	public Date getEndDate() {
		return endDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	public String getInstitution() {
		return institution;
	}

	public void setInstitution(String institution) {
		this.institution = institution;
	}

	public Country getCountry() {
		return country;
	}

	public void setCountry(Country country) {
		this.country = country;
	}

	public City getCity() {
		return city;
	}

	public void setCity(City city) {
		this.city = city;
	}

	public EducationType getType() {
		return type;
	}

	public void setType(EducationType type) {
		this.type = type;
	}

	public String addEducation() {
		Education education = new Education();
		education.setEducationCountry(country);
		education.setEducationCity(city);
		education.setEducationStart(startDate);
		education.setEducationEnd(endDate);
		education.setEducationType(type);
		education.setInstitutionTitle(institution);
		education.setUserEducation(userBean.getSelf());
		try {
			educationService.createEntity(education);
		} catch (UserException e) {
			LOGGER.error(ADD_USER_EDUCATION_FAILED, e);
		}
		return USER_MAIN;
	}

	public String removeEducation() {
		educationService.deleteEntityById(currentEducation.getEducationId());
		return USER_MAIN;
	}

}
